﻿using System;
using System.Collections.Generic;
using System.Text;
using XMK.Framework.Model.Ajax;
using XMK.Framework.Config;
using XMK.Utils;
using System.Reflection;
using XMK.Framework.Model;

namespace XMK.Framework.Web
{
    internal class AjaxDataMappingHelper
    {
        #region 私有变量
        private static readonly string CacheDataCallID = "XMK.Framework.AJAX.CACHE.DATA";
        private static readonly string CacheDataIsCallData = "XMK.Framework.AJAX.CACHE.DATA.ISCALLDATA";
        #endregion
        /// <summary>
        /// 根据别名获取方法
        /// </summary>
        /// <param name="methodAlias">Ajax别名</param>
        /// <returns>调用方法</returns>
        internal static ICallService GetAjaxDataMethod(string methodAlias)
        {
            Dictionary<string, ICallService> dict = CacheHelper<Dictionary<string, ICallService>>.GetCacheValue(CacheDataCallID);
            if (dict.ContainsKey(methodAlias))
                return dict[methodAlias];
            else
                return null;
        }

        /// <summary>
        /// 是否允许自动调用数据层
        /// </summary>
        /// <returns></returns>
        internal static bool GetIsCallData()
        {
            string s = CacheHelper<string>.GetCacheValue(CacheDataIsCallData);
            return s == "1";
        }
        /// <summary>
        /// 解析AjaxDataMapping.xml文件
        /// </summary>
        /// <returns>AjaxDataMapping.xml的解析后对象</returns>
        internal static AjaxDataMappingInfo LoadAjaxMapping(string mappingFilePath)
        {
                 return XmlHelper.Deserialize<AjaxDataMappingInfo>(mappingFilePath);
        }
        /// <summary>
        /// 把ajax方法配置转换成实际的对象并存储在缓存中
        /// </summary>
        /// <param name="ajaxMappingInfos">AjaxMapping.xml的解析后对象</param>
        internal static void InitAjaxDataMethod(AjaxDataMappingInfo ajaxMappingInfos)
        {
            CacheHelper<string>.SetCache(CacheDataIsCallData, ajaxMappingInfos.IsCallData);
            
            Dictionary<string, ICallService> typeDict = new Dictionary<string, ICallService>();
            
            foreach (DataMappingInfo info in ajaxMappingInfos.DataMappings)
            {
                if (info.WsIDMappings != null && info.WsIDMappings.Count > 0)
                {
                    ICallService callservice;
                    if (string.IsNullOrEmpty(info.RemotingUrl))
                    {
                        string[] arr = info.Name.Split(',');
                        callservice = (ICallService)ReflectionHelper.CreateInstance(arr[0], arr[1], null);
                    }
                    else
                    {
                        callservice = RemotingHelper.GetObject<ICallService>(info.RemotingUrl);
                    }

                    foreach (WsIDMappingInfo ws in info.WsIDMappings)
                    {
                        typeDict.Add(ws.id, callservice);
                    }
                }
            }
            CacheHelper<Dictionary<string, ICallService>>.SetCache(CacheDataCallID, typeDict);
        }
    }
}
